//
// Created by itworker on 23-5-31.
//

#ifndef UDPPUNCHCLIENT_NAT_CLIENT_H
#define UDPPUNCHCLIENT_NAT_CLIENT_H

#include <thread>
#include <chrono>
#include <cstring>
#include <unistd.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/select.h>
#include "error_handle.h"
#include "msg_protocol.h"

#define COMMANDMAXC 25600
#define MAXRETRY    5

class NATClient
{
private:
    u_short m_sClientPort;
    u_short m_sServerPort;

    char m_sUserName[10];
    char m_sServerIP[20];

public:
    static UserList m_uClntList;
    static int      m_iClntSock;
    static bool     m_bRecvACK;

private:
    void command_exit();
    void command_getu();
    void command_send(char* commandLine);
    void command_tell(char* commandLine);

    // 接收到P2P的消息，发送ACK
    static void command_P2PRecvMessageHandle(sockaddr_in &remote, cltP2PMessage &recvbuf, socklen_t sinlen);
    // 接收到打洞命令，向指定的IP地址打洞
    static void command_P2PSomeoneWantToCallYouHandle(sockaddr_in &remote, cltP2PMessage &recvbuf, socklen_t sinlen);

    bool SendMessageTo_v1(char *username, char *message);
    bool SendMessageTo_v2(char *username, char *message, const char *pIP, u_short nPort);

public:
    NATClient();
    ~NATClient();

public:
    void init();
    void output_usage();
    int create_socket(int domain, int type, int protocol);
    void bind_socket(int sock);
    void connect_server(int sock, char* userName, char* servIp);

    static void RecvThreadProc();
    void ParseCommand(char * commandLine);
};


#endif //UDPPUNCHCLIENT_NAT_CLIENT_H
